﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace BOHVote.Core
{
    public class SQLAccess
    {
        public SQLAccess(string strSQLConnection)
        {
            SQLConnenction = strSQLConnection;
        }

        private string _SQLConnection = string.Empty;
        public string SQLConnenction
        {
            get { return _SQLConnection; }
            set { _SQLConnection = value; }
        }

        private Database _database = null;
        public Database database
        {
            get
            {
                if (_database == null)
                {
                    _database = DatabaseProvider.CreateDatabase(SQLConnenction);
                }
                return _database;
            }
        }

        #region <<  DataSet  Operation  >>

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTableName">DB Table Name</param>
        /// <param name="strCondition">select condition e.g. id='1' and name<> '' </param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(string strTableName, string strCondition)
        {
            string strSQLScript = string.Empty;
            strSQLScript = string.Format("SELECT * FROM {0} WHERE {1} ");

            return ExecuteDataSet(strSQLScript);
        }

        /// <summary>
        /// get all record by select script
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(string strSQL)
        {
            DataSet dst = null;
            if (database != null)
            {
                DbCommand command = database.GetSqlStringCommand(strSQL);
                dst = database.ExecuteDataSet(command);
            }
            return dst;
        }

        #endregion

        #region << Reader Operation  >>
        public IDataReader ExecuteReader(string strSQL)
        {
            IDataReader datareader = null;
            if (database != null)
            {
                DbCommand command = database.GetSqlStringCommand(strSQL);
                datareader = database.ExecuteReader(command);
            }
            return datareader;
        }
        #endregion

        #region << Transaction Operation  >>

        public int ExecuteNonquery(string strSQL)
        {
            // DbTransaction 
            return 1;
        }

        #endregion

        
    }
}
